package com.groot.medical.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.groot.common.util.MedicalDBUtil;
import com.groot.medical.entity.SysUser;
import com.groot.medical.entity.UserRole;

public class UserDaoImpl implements UserDao {
	Connection conn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	
	
	@Override
	public SysUser loginById(String userId,String password) {
		/*Connection conn = null;*/
		try {
			conn = MedicalDBUtil.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		/*PreparedStatement ps = null;
		ResultSet rs = null;*/
		
		String sql = "select * from t_user where user_id=? and password=?";
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, userId);
			ps.setString(2, password);
			rs = ps.executeQuery();
			SysUser sysUser = null;
			if(rs.next()){
				/*sysUser = new SysUser(rs.getString(2), rs.getString(3), rs.getString(4));*/
				sysUser = new SysUser();
				sysUser.setUserId(rs.getString(1));
				sysUser.setPassword(rs.getString(2));
				sysUser.setUsername(rs.getString(3));
				sysUser.setAreaCode(rs.getString(4));
			}
			return sysUser;
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new  RuntimeException("出现错误！");
		}finally{
			MedicalDBUtil.closeAll(conn, ps, rs);
		}
	


	}
	
	@Override
	public int getALLUserCount() {
		int i =0;
		try {
			conn = MedicalDBUtil.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
		conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
		String sql="SELECT count(1) AS id FROM t_user";
		ps=conn.prepareStatement(sql);
		rs=ps.executeQuery();
		if(rs.next()){
			i=rs.getInt("id");
		}
		} catch (SQLException e) {

		e.printStackTrace();
		}
		finally{
			MedicalDBUtil.closeAll(conn, ps, rs);
		}
		return i;
		
		
		
		}
			
		

	@Override
	public int addUser(SysUser sysUser) {
		/*Connection conn=null;
		PreparedStatement ps = null;*/
		
		int i =0;
		try {
			conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
			String sql="insert into t_user(user_id,password,username,area_code)values(?,?,?,?)";
			ps=conn.prepareStatement(sql);
			ps.setString(1,sysUser.getUserId());
			ps.setString(2, sysUser.getPassword());
			ps.setString(3, sysUser.getUsername());
			ps.setString(4, sysUser.getAreaCode());
			i= ps.executeUpdate();
			
			} catch (SQLException e) {

			e.printStackTrace();
			}finally{
				MedicalDBUtil.closeAll(conn, ps, rs);
			}
			return i;
			
	}

	@Override
	public SysUser findUserById(String userid) {
		try {
			conn = MedicalDBUtil.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		/*PreparedStatement ps = null;
		ResultSet rs = null;*/
		
		String sql = "select * from t_user where user_id=?";
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, userid);
			rs = ps.executeQuery();
			/*SysUser sysUser = null;*/
			if(rs.next()){
				/*sysUser = new SysUser(rs.getString(2), rs.getString(3), rs.getString(4));*/
				SysUser sysUser = new SysUser();
				sysUser.setUserId(rs.getString(1));
				sysUser.setPassword(rs.getString(2));
				sysUser.setUsername(rs.getString(3));
				return sysUser;
			}
			
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new  RuntimeException("出现错误！");
		}finally{
			MedicalDBUtil.closeAll(conn, ps, rs);
		}
		return null;
	}

	@Override
	public int editUser(SysUser sysUser) {
		/*Connection conn=null;
		PreparedStatement ps = null;*/
		
		int i =0;
		try {
			conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
			//UPDATE student SET name='"+name+"', age=" + age+" where name='大红'";
			String sql="update t_user set password=?,username=? where user_id=?";
			ps=conn.prepareStatement(sql);
			
			ps.setString(1, sysUser.getPassword());
			ps.setString(2, sysUser.getUsername());
			ps.setString(3,sysUser.getUserId());
			i= ps.executeUpdate();
			
			} catch (SQLException e) {

			e.printStackTrace();
			}finally{
				MedicalDBUtil.closeAll(conn, ps, rs);
			}
			return i;
	}

	@Override
	public List<SysUser> findUserByPage(int startIndex, int pageSize) {
		try {
			conn = MedicalDBUtil.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		String sql = "select * from t_user limit ?,?";
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, startIndex);
			ps.setInt(2, pageSize);
			rs = ps.executeQuery();
			List<SysUser> userList=new ArrayList<SysUser>();
			while(rs.next()){
				/*sysUser = new SysUser(rs.getString(2), rs.getString(3), rs.getString(4));*/
				SysUser sysUser = new SysUser();
				sysUser.setUserId(rs.getString(1));
				sysUser.setPassword(rs.getString(2));
				sysUser.setUsername(rs.getString(3));
				userList.add(sysUser);
				
			}
			
			return userList;
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new  RuntimeException("出现错误！");
		}finally{
			MedicalDBUtil.closeAll(conn, ps, rs);
		}
		
	}

	@Override
	public void addUserAndMenu(UserRole userRole) {
		try {
			conn = MedicalDBUtil.getConnection();
			String[] split =userRole.getRoleId().split(",");
			for(int i=0;i<split.length;i++){
				String sql="insert into t_user_role(user_id,role_id)values(?,?)";
				ps = conn.prepareStatement(sql);
				ps.setString(1, userRole.getUserId());
				ps.setString(2, split[i]);
				ps.executeUpdate();
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			MedicalDBUtil.closeAll(conn, ps, rs);
		}
		
		
	}

	@Override
	public void delUserRole(String userid) {
		
		try {
			conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
			String sql="delete from t_user_role where user_id=?";
			ps=conn.prepareStatement(sql);
			ps.setString(1, userid);
			ps.executeUpdate();
			
			} catch (SQLException e) {

			e.printStackTrace();
			}finally{
				MedicalDBUtil.closeAll(conn, ps, rs);
			}
			
	}

	@Override
	public int deleteUserByUserId(String userid) {
		int i=0;
		try {
			conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
			String sql="delete from t_user where user_id=?";
			ps=conn.prepareStatement(sql);
			ps.setString(1, userid);
			i=ps.executeUpdate();
			
			} catch (SQLException e) {

			e.printStackTrace();
			}finally{
				MedicalDBUtil.closeAll(conn, ps, rs);
			}
		return i;
	}
	

}
